/*
 *      Copyright 2007 Nikolai Ugelvik <nikko-u@online.no>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 * 		See the 'LICENSE' file for further information.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

/* Standard libraries */
#include <stdio.h>
#include <string.h>

/* Own includes */
#include "../include/global.h"
#include "../include/loadpng.h"
#include "../include/mainmenu.h"
#include "../include/xmlparser.h"

/* Defines */
#define DEFAULT_RESOLUTION_X 320
#define DEFAULT_RESOLUTION_Y 200
#define DEFAULT_COLORDEPTH 16

void ticker()
{
  ticks++;
}
END_OF_FUNCTION(ticker);

int do_setup(int argn, char * argv[], int * hwmouse)
{
	/* Setup allegro */
	if(allegro_init() != 0)
	{
		printf("ERROR: Could not initialize allegro.\nTerminating.\n");
		return 1;
	}

    /* Check params */
	int windowstatus = GFX_AUTODETECT_WINDOWED;
	for(int I = 0; I < argn; I++)
	{
		if(!strcmp(argv[I], "-f")) windowstatus = GFX_AUTODETECT_FULLSCREEN;
		if(!strcmp(argv[I], "-d")) debugmode = 1;
		else if(!strcmp(argv[I], "--help"))
		{
			printf("Command line options: \n\n");
			printf("-f\t- Fullscreen mode\n");
            printf("-d\t- Debug mode\n");
			printf("\n");
			return 1;
		}
	}

	debugmode = 1; /* TODO (nikko#1#): Remove this (force debugmode) sooner or later */

	/* Timer */
	if(install_timer() != 0)
	{
		printf("ERROR: Could not initialize timer.\nTerminating.\n");
		return 1;
	}
	LOCK_VARIABLE(ticks);
	LOCK_FUNCTION(ticker);
	install_int_ex(ticker, BPS_TO_TIMER(60));

    /* Create a windowed viewport from xml settings file */
    xmlDocPtr doc = NULL;
    xmlNodePtr node = NULL;
    int resx, resy, coldep;

    if((doc = xml_loaddoc("xml/settings.xml")))
    {
        if((node = xml_getroot(doc, "settings")))
        {
            node = xml_getnode(node, "resolution");

            resx = atoi(xml_readnode(doc, node, "x"));
            resy = atoi(xml_readnode(doc, node, "y"));
            coldep = atoi(xml_readnode(doc, node, "colordepth"));

            printf("Running at %dx%dx%d\n", resx, resy, coldep);
        } else {
            printf("WARNING: settings.xml is NOT valid. Running default resolution.\n");
            resx = DEFAULT_RESOLUTION_X;
            resy = DEFAULT_RESOLUTION_Y;
            coldep = DEFAULT_COLORDEPTH;
        }
    } else {
        printf("WARNING: XML not found. Running default resolution.\n");
        resx = DEFAULT_RESOLUTION_X;
        resy = DEFAULT_RESOLUTION_Y;
        coldep = DEFAULT_COLORDEPTH;
    }

    xmlFree(doc);
    xmlFree(node);

	set_color_depth(coldep);
	if(set_gfx_mode(windowstatus, resx, resy, 0, 0) != 0)
	{
		printf("ERROR: Could not initialize graphical window.\nTerminating.\n");
		return 1;
	}

	/* Create a buffer */
	buffer = create_bitmap(resx, resy);
	clear_to_color(buffer, makecol(0, 0, 255));

	/* Register PNG format */
	register_png_file_type();

	/* Start accepting keyboard input */
	if(install_keyboard() != 0)
	{
		printf("ERROR: Could not initialize keyboard.\nTerminating.\n");
		return 1;
	}

    /* Start accepting mouse input */
	install_mouse();
	enable_hardware_cursor();

	/* generate a mouse cursor Lucasarts style */
	static BITMAP * cursor_bmp = 0;
	cursor_bmp = create_bitmap(32, 32);
   	clear_to_color(cursor_bmp, bitmap_mask_color(screen));
   	for (int i = 0; i<32; i++) {
   		if(abs(i - 16) < 3 || i < 6 || i > 32 - 6) continue;	// Empty in the center
      		putpixel(cursor_bmp, i, 16, makecol(255, 255, 255));
		putpixel(cursor_bmp, 16, i, makecol(255, 255, 255));
	}

   	/* select the custom cursor and set the focus point to the middle of it */
   	set_mouse_sprite(cursor_bmp);
   	set_mouse_sprite_focus(16, 16);

	show_mouse(screen);	/* <- should not go to the screen, if HARDWARE mouse is unavailable but ignore for now */

	/* check if HW cursor is supported. Display mode has to be set first */
	if ((*hwmouse = ((gfx_capabilities & GFX_HW_CURSOR) == GFX_HW_CURSOR))) {
	} else {
		printf("Hardware cursor NOT supported");
	}

	return 0;
}

void do_cleanup()
{
	destroy_bitmap(buffer);
}

void sync()
{
	blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
}

void cls(int dosync)
{
    clear_bitmap(buffer);

    if(dosync == 1)
        sync();
}

int main(int argn, char * argv[])
{
    int hwmouse = 1;

	if(do_setup(argn, argv, &hwmouse) != 0)
		return 1;

	main_menu();

	do_cleanup();

	return 0;
}
END_OF_MAIN()
